<%@ page import="net.tutwiz.model.Step" %>
<%@ page import="net.tutwiz.model.Tutorial" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.gson.FieldAttributes" %>
<%@ page import="net.tutwiz.model.User" %>
<%@ page import="com.google.gson.GsonBuilder" %>
<%@ page import="com.google.gson.ExclusionStrategy" %>
<%@ page import="com.google.gson.Gson;" %>
<%
	response.setContentType("application/json");
	response.setCharacterEncoding("utf-8");
%>

<%
	String returnValue = "{ \"allTutorials\":[";
	for(Tutorial t : (List<Tutorial>)request.getAttribute("parameters")) {
		returnValue += "{";
		returnValue += "\"id\":"			+ t.getId() + ", ";
		returnValue += "\"title\":\""		+ t.getTitle() + "\", ";
		returnValue += "\"description\":\""	+ t.getDescription() + "\", ";
		returnValue += "\"rate\":"			+ t.getRate() + ", ";
		returnValue += "\"createdAt\":\""			+ t.getCreatedAt() + "\", ";
		returnValue += "\"createdBy\": {";
		returnValue +=					"\"name\":\"" + t.getCreatedBy().getName() + "\", ";
		returnValue +=					"\"id\":" + t.getCreatedBy().getID();
		returnValue +=					"},";
		returnValue += "\"steps\": [";
		for(Step s : t.getSteps()) {
			returnValue += "{\"id\":" + s.getId() + ", \"title\":\"" + s.getTitle() + "\"},";
		}
		returnValue = returnValue.substring(0, returnValue.length()-1);
		returnValue += "]},";
		
	}
	returnValue = returnValue.substring(0, returnValue.length()-1);
	returnValue += "]}";

/*    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
       
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;//(clazz == User.class);
        }
        
        /**
          * Custom field exclusion goes here
          */
/*        public boolean shouldSkipField(FieldAttributes f) {
            return f.getName().equals("tutorial") || f.getName().equals("tutorials");
        }

     }).serializeNulls().create();
    String jsonResponse = gson.toJson(request.getAttribute("parameters"));*/
%>
<%= returnValue %>